import { Controller } from "../../mvc/core/Controller";
import { InvestmentPlanHintBtn } from "../view/investmentPlan/InvestmentPlanHintBtn";
import { ResFileConst } from "../resource/ResFileConst";
import { HintBtnBase } from "../view/HintBtnBase";
import { InvestmentPlanModule } from "../../../modules/InvestmentPlanModule";
import { IView } from "../../mvc/interfaces/IView";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ClockManager } from "../manager/ClockManager";
import { TInvestPlan } from "../../../Message/Db/Tables/TInvestPlan";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { InvestmentConfig } from "../resource/InvestmentConfig";
import { Language } from "../../../extend/language/Language";
import { SHaveInvestInfo } from "../../../Message/Public/SHaveInvestInfo";
import { SInvestPlanInfo } from "../../../Message/Public/SInvestPlanInfo";
type int = number;
//class InvestmentPlanController
    
    export  class InvestmentPlanController extends Controller
    {
       

        /* internal  */get icoBtn(): InvestmentPlanHintBtn
        {
            if (this._icoBtn == null) 
            {
                this._icoBtn = new InvestmentPlanHintBtn(ResFileConst.InvestHintBtn, HintBtnBase.SHINE_CLICK);
            }
            return this._icoBtn;
        }

        /* internal  */get module(): InvestmentPlanModule
        {
            return this.view as InvestmentPlanModule;
        }

        protected /* override */ initView(): IView
        {
            if (!this._view) 
            {
                this._view = new InvestmentPlanModule();
            }
            return this._view;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.OnInvestPlanInfo, this.onInvestInfoReady);
            NetDispatcher.addCmdListener(ServerCommand.OnInvestSuccess, this.onInvestSuccess);
            NetDispatcher.addCmdListener(ServerCommand.InvestPlanIsOpen, this.onActiveOpen);
            Dispatcher.addEventListener(EventName.OpenInvestmentPlan, this.openInvestModule);
            Dispatcher.addEventListener(EventName.InvestProject, this.investPrj);
            Dispatcher.addEventListener(EventName.CurWorldLevelUpdate, this.onCurWorldLevelUpdateHandler);
            return;
        }

        /* internal  */onCurWorldLevelUpdateHandler(arg1: DataEvent): void
        {
            if (this.activeIsOpen && cache.role.curWorldLevel >= 40) 
            {
                this.icoBtn.show();
            }
            else 
            {
                this.icoBtn.hide();
            }
            return;
        }

        /* internal  */onActiveOpen(arg1: SActiveToClient): void
        {
            if (arg1.endDt > ClockManager.instance.nowDate) 
            {
                this.activeIsOpen = true;
                if (cache.role.curWorldLevel >= 40) 
                {
                    this.icoBtn.show();
                }
            }
            return;
        }

        /* internal  */investPrj(arg1: DataEvent): void
        {
            var e: DataEvent;
            var id: int;
            var info: TInvestPlan;
            var onAnser: Function;

            var loc1;
            id = 0;
            onAnser = null;
            e = arg1;
            onAnser = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    GameProxy.active.invest(id);
                }
                return;
            }
            id = e.data as int;
            info = InvestmentConfig.instance.getInfoById(id);
            if (info) 
            {
                Alert.show(Language.getStringByParam(90805, info.goldPay.toString()), null, Alert.OK | Alert.CANCEL, null, onAnser);
            }
            return;
        }

        /* internal  */openInvestModule(arg1: DataEvent): void
        {
            GameProxy.active.getInvestPlanInfo();
            this.module.show();
            return;
        }

        /* internal  */onInvestSuccess(arg1: SHaveInvestInfo): void
        {
            var loc1=null;
            if (this.cache.investment.investPlanInfo) 
            {
                this.cache.investment.investPlanInfo.haveInvestInfos.push(arg1);
                loc1 = this.cache.investment.investPlanInfo.canInvestIds.concat();
                this.module.update(loc1);
            }
            return;
        }

        /* internal  */onInvestInfoReady(arg1: SInvestPlanInfo): void
        {
            this.cache.investment.investPlanInfo = arg1;
            if (this.module.isHide) 
            {
                this.module.show();
            }
            var loc1=this.cache.investment.investPlanInfo.canInvestIds.concat();
            this.module.update(loc1);
            return;
        }

        private /* var */_icoBtn: InvestmentPlanHintBtn;

        private /* var */activeIsOpen: Boolean=false;
    }
